<%@ Page language="c#" Codebehind="Hyperlink.aspx.cs" AutoEventWireup="True" Inherits="CrossFactory.SiteNote.XmlEditor.Hyperlink" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
	<head>
		<title>
			<%= pageResourceManager.GetString("pagetitle")%>
		</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		<LINK href="StyleSheets/XmlEditor.css" type="text/css" rel="stylesheet">
		<script language="javascript" src="HierarchyManager.js">
		</script>
		<script language="javascript">
			var xmlEditor;
			var hyperlink;
			var dialog = null;
			var oHyperlinkType;
			var oType;
			var oAddress;
			var oAnchor;
			var oChoosePage;
			var oTarget;
			var oTooltip;
			var bClosing;
			var hierarchyManager;
			var realUrl;
			var initialAddress;
			var hmAddress;
			var hyperlinkType;

			function Loaded()
			{
				var oOption;
				var pos;
				var isfile;
				
				bClosing = false;
				window.returnValue = false;

				hierarchyManager = new HierarchyManager(<%# ManagerParameters %>);
				
				xmlEditor = dialogArguments;

				hyperlink = xmlEditor.hyperlink;
				oHyperlinkType = document.all["hyperlinktype"];
				oType = document.all["type"];
				oAddress = document.all["address"];
				oAnchor = document.all["linkanchor"];
				oChoosePage = document.all["choosepage"];
				oTarget = document.all["target"];
				oTooltip = document.all["tooltip"];
				
				this.hyperlinkType = hyperlink.type;
				//alert(this.hyperlinkType);
				switch(hyperlink.type)
				{
					case 'page':
					case 'www,page':
					case 'www,page,file':
						oChoosePage.style.display = '';
					case 'www':
						oOption = document.createElement("OPTION");
						oOption.text="WWW";
						oOption.value="www";
						oType.add(oOption);
						oType.selectedIndex = 0;
				}

				//realAddress = null;
				initialAddress = hyperlink.address;
				//alert(initialAddress);
				hmAddress = "";
				if (hyperlink.address != null)
				{
				    isfile = initialAddress.indexOf("file:");
					
					if (isfile != -1)
					{
					    re = new RegExp('/', 'gi');
				        
						oAddress.value = initialAddress.replace(re,"\\");
					}
					else
					{
					    if (hyperlink.address.substr(0, hierarchyManager.baseUrl.length).toLowerCase() == hierarchyManager.baseUrl.toLowerCase())
					    {
						    oAddress.value = hyperlink.address.substr(hierarchyManager.baseUrl.length - 1);
					    }
					    else
					    {
						    oAddress.value = hyperlink.address;
					    }
					}
					hmAddress = oAddress.value;
				}
				
				if (hyperlink.linkanchor != null)
				{
					oAnchor.value = hyperlink.linkanchor;
				}

				if (hyperlink.target != null)
				{
					oTarget.value = hyperlink.target;
				}

				if (hyperlink.tooltip != null)
				{
					oTooltip.value = hyperlink.tooltip;
				}
				realUrl = hyperlink.realUrl;

				curHeight = parseInt(window.dialogHeight);
				curHeight = curHeight + document.body.firstChild.offsetHeight - document.body.clientHeight;
				window.dialogHeight = curHeight + 'px';
			}
			
			function ChoosePage()
			{
				var result;
				var address;
				var pos;
				var type;
				var cmId;
				var relUri;
				
				if(oAddress.value != "")
				{
				    result = hierarchyManager.ChoosePage(this.hyperlinkType,oAddress.value);
				}
				else
				{
				    try
				    {
				        relUri = document.location.href;
				        if(relUri.indexOf("?defaultUri=") >= 0)
				        {
				            relUri = relUri.substring(relUri.indexOf("?defaultUri=") + 12);
				        }
				    
				        //alert(relUri);
				        result = hierarchyManager.ChoosePage(this.hyperlinkType,relUri);
				    }
				    catch(e)
				    {
				        result = hierarchyManager.ChoosePage(this.hyperlinkType,oAddress.value);
				    }
				}
				
				if (result.returnValue)
				{
					pos = result.selectedId.indexOf(":");
					
					if (pos != -1)
					{
						type = result.selectedId.substr(0, pos).toLowerCase();
						
						switch(type)
						{
							case "cm":
								cmId = result.selectedId.substr(pos + 1);
								
								address = hierarchyManager.GetUrl(cmId);
								
								if (address != null)
								{
									if (address.substr(0, hierarchyManager.baseUrl.length).toLowerCase() == hierarchyManager.baseUrl.toLowerCase())
									{
										address = address.substr(hierarchyManager.baseUrl.length - 1);
									}

									realUrl = result.selectedId;
									oAddress.value = address;
									hmAddress = address;
								}
								break;
							case "file":
								realUrl = result.selectedId.replace(/&/, "&amp;");
								realUrl = realUrl.replace(/</, "&lt;");
								realUrl = realUrl.replace(/>/, "&gt;");

								address = realUrl.substr(pos + 1);
								oAddress.value = '<%= Application["ApplicationPath"] %>' + address;
								hmAddress = oAddress.value;
								break;
						}
					}
				}
			}
			
			function ShowUpdateFileWindow(propertyId, fileUri, mode)
			{
				//alert(fileUri);
				if (dialog != null)
				{
					if (!dialog.closed)
					{
						dialog.focus();
						return;
					}
					dialog = null;
				}
				
				currentFileUri = fileUri;
				/*if (currentSizeWidth != null)
				{
					if(document.all["xe_" + currentSizeWidth] != null)
					{
						currentFileWidth = document.all["xe_" + currentSizeWidth].value;
					}
				}
				if (currentSizeHeight != null)
				{
					if(document.all["xe_" + currentSizeHeight] != null)
					{
						currentFileHeight = document.all["xe_" + currentSizeHeight].value;
					}
				}*/
				currentPropertyId = propertyId;
				dialog = xmlEditor.window.open(xmlEditor.xmlEditorPath + 'Upload.aspx?mode=' + mode, null, "height=200px,width=480px,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=no,status=no,scrollbars=no");
				dialog.opener = window;
				dialog.xmlEditor = xmlEditor;
				dialog.moveTo((window.screen.availWidth - 400) / 2, (window.screen.availHeight - 150) / 2);
			}

			function Close()
			{
				if (!bClosing)
				{
					window.close();
				}
			}

			function Save()
			{
				var address;
				var pos;
				var type;
				var basePath;
				var fileAddress;
				var bFileSet;
			
				if (oAddress.value == '')
				{
					hyperlink.address = null;
				}
				else
				{
					address = oAddress.value;
					if(address.indexOf("#") != -1)
					{
						pos = address.indexOf("#");						
						if (oAnchor != null)
						{
							if (oAnchor.value == "")
							{
								oAnchor.value = address.substr(pos + 1)
							}
						}
						address = address.substr(0,pos);
					}

					pos = address.indexOf(":");
					
					if (pos != -1)
					{
						type = address.substr(0, pos).toLowerCase();
						switch(type)
						{
							case "mailto":
								hyperlink.address = address;
								if(oAnchor != null)
								{
									hyperlink.linkanchor = oAnchor.value;
								}
								else
								{
									hyperlink.linkanchor = null;
								}
								hyperlink.realUrl = oAddress.value;
							break;
							case "file":
								hyperlink.address = address;
								hyperlink.realUrl = oAddress.value;
							break;
							case "http":
							case "https":
								if (address.substr(0, hierarchyManager.baseUrl.length).toLowerCase() == hierarchyManager.baseUrl.toLowerCase())
								{
									address = address.substr(hierarchyManager.baseUrl.length - 1);
									nodeId = hierarchyManager.GetNodeId(address);
									if (nodeId != null)
									{	
										realUrl = hierarchyManager.name + ':' + hierarchyManager.GetNodeId(address);
										hyperlink.address = address;
										if(oAnchor != null)
										{
											hyperlink.linkanchor = oAnchor.value;
										}
										else
										{
											hyperlink.linkanchor = null;
										}
										hyperlink.realUrl = realUrl;
									}
									else
									{
										hyperlink.realUrl = address;
									}
								}
								else
								{
								
									hyperlink.address = address;
									if(oAnchor != null)
									{
										hyperlink.linkanchor = oAnchor.value;
									}
									else
									{
										hyperlink.linkanchor = null;
									}
									
									hyperlink.realUrl = oAddress.value;
								}
							break;
						}
					}
					else
					{
						if (address == initialAddress)
						{
								if(oAnchor != null)
								{
									hyperlink.linkanchor = oAnchor.value;
								}
								else
								{
									hyperlink.linkanchor = null;
								}
						}
						else
						{
							bFileSet = false;
							
							basePath = '<%= Application["ApplicationPath"] %>';
							if (hmAddress == oAddress.value)
							{
								if (realUrl.substr(0, 5).toLowerCase() == "file:")
								{
									hyperlink.address = address;
									hyperlink.realUrl = "file:" + address.substr(basePath.length);
									if(oAnchor != null)
									{
										hyperlink.linkanchor = oAnchor.value;
									}
									else
									{
										hyperlink.linkanchor = null;
									}
									bFileSet = true;
								}
							}
							if (!bFileSet)
							{
							
								if (address.substr(0, basePath.length).toLowerCase() == basePath.toLowerCase())
								{
									if(oAnchor != null)
									{
										hyperlink.linkanchor = oAnchor.value;
									}
									else
									{
										hyperlink.linkanchor = null;
									}
									fileAddress = address;
									address = address.substr(basePath.length - 1);
									realUrl = hierarchyManager.name + ':' + hierarchyManager.GetNodeId(address);
									if (realUrl == "cm:null")
									{
										hyperlink.address = fileAddress;
										hyperlink.realUrl = "file:" + address.substr(1);
									}
									else
									{
										hyperlink.address = address;
										hyperlink.realUrl = realUrl;
									}
								}
								else
								{
									if (address.substr(0, 1).toLowerCase() == "/")
									{
										if(oAnchor != null)
										{
											hyperlink.linkanchor = oAnchor.value;
										}
										else
										{
											hyperlink.linkanchor = null;
										}
										fileAddress = address;
										realUrl = hierarchyManager.name + ':' + hierarchyManager.GetNodeId(address);
										if (realUrl == "cm:null")
										{
											hyperlink.address = fileAddress;
											hyperlink.realUrl = "file:" + address.substr(1);
										}
										else
										{
											hyperlink.address = address;
											hyperlink.realUrl = realUrl;
										}
									}
									else
									{
										hyperlink.address = "http://" + address;
										if(oAnchor != null)
										{
											hyperlink.linkanchor = oAnchor.value;
										}
										else
										{
											hyperlink.linkanchor = null;
										}
										hyperlink.realUrl = "http://" + address;
									}
								}
							}
						}
					}
				}
				
				hyperlink.target = oTarget.value;
				hyperlink.tooltip = oTooltip.value;
				
				
				bClosing = true;
				window.returnValue = true;
				window.close();
			}

			function UnLoading()
			{
				bClosing = true;
  			}
		</script>
	</head>
	<body onload="Loaded()" onunload="UnLoading()">
		<table id="Main" cellSpacing="0" cellPadding="0" border="0" style="height:100%;width:100%;">
			<tr height="31">
				<td colspan="5">
					<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
						<tr>
							<td width="100%">
								<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
									<tr>
										<td width="100%" background="Images/HeaderLeft.gif"><img src="Images/Transp.gif" height="1" width="100%"></td>
										<td align="right"><img src="Images/HeaderRight.gif"></td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td width="7" background="Images/BackgroundLeft.gif"><img src="Images/Transp.gif" width="7"></td>
				<td width="7"><img src="Images/Transp.gif" width="7"></td>
				<td id="content" width="100%" height="100%" style="padding-bottom:7px;" valign="top" class="label">
					<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
						<tr id="hyperlinktype" style="display:none;">
							<td class="label"><nobr><%= pageResourceManager.GetString("type")%></nobr></td>
							<td class="label">&nbsp;:&nbsp;</td>
							<td class="value" colspan="2"><select id="type" class="value"></select></td>
						</tr>
						<tr>
							<td class="label"><nobr><%= pageResourceManager.GetString("address")%></nobr></td>
							<td class="label">&nbsp;:&nbsp;</td>
							<td class="value" width="100%"><input id="address" type="text" class="value"></input></td>
							<td class="label"><input id="choosepage" type="button" style="display:none;" class="btnValue" name="ChoosePage" value='<%= pageResourceManager.GetString("chooselink")%>' onclick="ChoosePage();" /></td>
						</tr>
						<tr>
							<td class="label"><nobr><%= pageResourceManager.GetString("anchor")%></nobr></td>
							<td class="label">&nbsp;:&nbsp;</td>
							<td class="value" colspan="2"><input id="linkanchor" type="text" class="value"></input></td>
						</tr>
						<tr>
							<td class="label"><nobr><%= pageResourceManager.GetString("target")%></nobr></td>
							<td class="label">&nbsp;:&nbsp;</td>
							<td class="value" colspan="2">
								<select id="target" class="value">
									<option value="_parent"><%= pageResourceManager.GetString("_parent")%></option>
									<option value="_blank"><%= pageResourceManager.GetString("_blank")%></option>
								</select>
							</td>
						</tr>
						<tr>
							<td class="label"><nobr><%= pageResourceManager.GetString("tooltip")%></nobr></td>
							<td class="label">&nbsp;:&nbsp;</td>
							<td class="value" colspan="2"><input id="tooltip" type="text" class="value"></input></td>
						</tr>
					</table>
				</td>
				<td width="7"><img src="Images/Transp.gif" width="7"></td>
				<td width="9" height="100%">
					<table border="0" cellpadding="0" cellspacing="0" width="9" height="100%">
						<tr height="32">
							<td width="9"><img src="Images/BackgroundTopRight.gif"></td>
						</tr>
						<tr height="100%">
							<td width="9" background="Images/BackgroundRight.gif"></td>
						</tr>
					</table>
				</td>
			</tr>
			<tr height="40">
				<td colspan="5">
					<table border="0" cellpadding="0" cellspacing="0" height="100%">
						<tr>
							<td height="8"><img src="Images/FooterLeft.gif"></td>
							<td width="100%">
								<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
									<tr>
										<td width="100%" background="Images/FooterCenter.gif" height="8"></td>
										<td height="8"><img src="Images/FooterRight.gif" width="255" height="8"></td>
									</tr>
								</table>
							</td>
						</tr>
						<tr>
							<td align="right" width="100%" colSpan="3">
								<table id="Table5" width="100%" height="100%" border="0" style="background :url(Images/Footer.gif) no-repeat right; ">
									<tr>
										<td width="100%"></td>
										<td><input id="Save" type="button" name="Save" value='<%= pageResourceManager.GetString("saveButton")%>' onclick="Save();" /></td>
										<td><input id="Cancel" type="button" name="Close" value='<%= pageResourceManager.GetString("cancelButton")%>' onclick="Close();" /></td>
										<td width="1"></td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</body>
</html>
